home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Internet / gpodder / gpodder-3.8.3-setup.exe / {app} / src / mygpoclient / util.py < prev   
Text File  |  2013-02-08  |  3KB  |  90 lines

  1. # -*- coding: utf-8 -*-
  2. # gpodder.net API Client
  3. # Copyright (C) 2009-2013 Thomas Perl and the gPodder Team
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  
  18. import mygpoclient
  19.  
  20. import datetime
  21.  
  22. def join(*args):
  23.     """Join separate URL parts to a ful URL"""
  24.     return '/'.join(args)
  25.  
  26. def iso8601_to_datetime(s):
  27.     """Convert a ISO8601-formatted string to datetime
  28.  
  29.     >>> iso8601_to_datetime('2009-12-29T19:25:33')
  30.     datetime.datetime(2009, 12, 29, 19, 25, 33)
  31.     >>> iso8601_to_datetime('2009-12-29T19:25:33Z')
  32.     datetime.datetime(2009, 12, 29, 19, 25, 33)
  33.     >>> iso8601_to_datetime('xXxXxXxXxxxxXxxxXxx')
  34.     >>>
  35.     """
  36.     for format in ('%Y-%m-%dT%H:%M:%S', '%Y-%m-%dT%H:%M:%SZ'):
  37.         try:
  38.             return datetime.datetime.strptime(s, format)
  39.         except ValueError:
  40.             continue
  41.  
  42.     return None
  43.  
  44. def datetime_to_iso8601(dt):
  45.     """Convert a datetime to a ISO8601-formatted string
  46.  
  47.     >>> datetime_to_iso8601(datetime.datetime(2009, 12, 29, 19, 25, 33))
  48.     '2009-12-29T19:25:33'
  49.     """
  50.     return dt.strftime('%Y-%m-%dT%H:%M:%S')
  51.  
  52. def position_to_seconds(s):
  53.     """Convert a position string to its amount of seconds
  54.  
  55.     >>> position_to_seconds('00:00:01')
  56.     1
  57.     >>> position_to_seconds('00:01:00')
  58.     60
  59.     >>> position_to_seconds('01:00:00')
  60.     3600
  61.     >>> position_to_seconds('02:59:59')
  62.     10799
  63.     >>> position_to_seconds('100:00:00')
  64.     360000
  65.     """
  66.     hours, minutes, seconds = (int(x) for x in s.split(':', 2))
  67.     return (((hours*60)+minutes)*60)+seconds
  68.  
  69. def seconds_to_position(seconds):
  70.     """Convert the amount of seconds to a position string
  71.  
  72.     >>> seconds_to_position(1)
  73.     '00:00:01'
  74.     >>> seconds_to_position(60)
  75.     '00:01:00'
  76.     >>> seconds_to_position(60*60)
  77.     '01:00:00'
  78.     >>> seconds_to_position(59 + 60*59 + 60*60*2)
  79.     '02:59:59'
  80.     >>> seconds_to_position(60*60*100)
  81.     '100:00:00'
  82.     """
  83.     minutes = int(seconds/60)
  84.     seconds = seconds % 60
  85.     hours = int(minutes/60)
  86.     minutes = minutes % 60
  87.     return '%02d:%02d:%02d' % (hours, minutes, seconds)
  88.  
  89.  
  90.